/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validation;

import model.UserBean;

/**
 *
 * @author Jagadeesh
 */
public class ValidateForm {

    UserBean validBean = new UserBean();
private UserBean user;
    public UserBean validateData(UserBean ubean) {
        validBean.setIsValid(true);
        //if given username value is not valid then seterror message and set empty value to input
        //field
         if (ubean.getfullName().length() == 0) {
            validBean.setfullNameError("please enter valid name");
            validBean.setIsValid(false);
            validBean.setfullName("");
        } 
         //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setfullNameError("");
            //  validBean.setIsValid(true);
            validBean.setfullName(ubean.getfullName());
        }
        if (ubean.getUserName().length() == 0) {
            validBean.setUserNameError("please enter valid name");
            validBean.setIsValid(false);
            validBean.setUserName("");
        } else if (!ubean.getUserName().matches("[a-zA-Z]*")) {
            validBean.setUserNameError("please enter valid name");
            validBean.setIsValid(false);
            validBean.setUserName("");

        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setUserNameError("");
            //  validBean.setIsValid(true);
            validBean.setUserName(ubean.getUserName());
        }
        if (ubean.getPassword().length() == 0 && ubean.getPassword().length() < 8) {
            validBean.setPasswordError("please enter valid password min 8 character");
            validBean.setIsValid(false);
            validBean.setPassword("");
        } else {
            validBean.setPasswordError("");
            //  validBean.setIsValid(true);
            validBean.setPassword(ubean.getPassword());
        }
        //if given username value is not valid then seterror message and set empty value to input
        //field
        if (!ubean.getDateOfBirth().matches("\\d{4}-\\d{1,2}-\\d{1,2}")) {
            validBean.setDateOfBirthError("please enter valid date ex 1992-12-12");
            validBean.setIsValid(false);
            validBean.setDateOfBirth("");

        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setDateOfBirthError("");
            //   validBean.setIsValid(true);
            validBean.setDateOfBirth(ubean.getDateOfBirth());
        }

        //if given username value is not valid then seterror message and set empty value to input
        //field
        System.out.println("email" + ubean.getEmail());
        System.out.println(!ubean.getEmail().matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$"));
        if (!ubean.getEmail().matches("^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$")) {
            validBean.setEmailError("please enter valid email");
            validBean.setIsValid(false);
            validBean.setEmail("");
        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setEmailError("");
            //   validBean.setIsValid(true);
            validBean.setEmail(ubean.getEmail());
        }

        //if given username value is not valid then seterror message and set empty value to input
        //field
        if (ubean.getPhoneNo().length() < 8) {
            validBean.setPhoneNoError("please enter valid phoneno");
            validBean.setIsValid(false);
            validBean.setPhoneNo("");
        } //if given username value is valid then seterror message to empty and set value to bean
        else {
            validBean.setPhoneNoError("");
            //    validBean.setIsValid(true);
            validBean.setPhoneNo(ubean.getPhoneNo());
        }
        return validBean;
    }

    public UserBean ValidateLogin(UserBean bean) {

        return validBean;

    }

    public boolean isValidUser(String userName, String password) {
        return userName.equals(validBean.getUserName()) && password.equals(validBean.getPassword());
    }

   public UserBean getUser(){
       return user;
   }
}
